package com.game.server.db.dao;

import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.XyClubContributionLog;
import com.game.server.db.entity.XyClubPremiumOplog;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.sql.rowset.CachedRowSet;
import java.sql.SQLException;
import java.sql.Types;

public class XyClubContributionLogDao {

    public static void insertInto(XyClubContributionLog info) {
        String sql = "INSERT INTO xy_club_contribution_log (cid,pid,uid,relevance_id,integral,proportion,val,createdate,roomkey) VALUES (?,?,?,?,?,?,?,NOW(),?)";
        Parameter p1 = new Parameter(Types.INTEGER, info.getCid(), ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, info.getPid(), ParameterType.Input);
        Parameter p3 = new Parameter(Types.INTEGER, info.getUid(), ParameterType.Input);
        Parameter p4 = new Parameter(Types.INTEGER, info.getRelevance_id(), ParameterType.Input);
        Parameter p5 = new Parameter(Types.INTEGER, info.getIntegral(), ParameterType.Input);
        Parameter p6 = new Parameter(Types.INTEGER, info.getProportion(), ParameterType.Input);
        Parameter p7 = new Parameter(Types.INTEGER, info.getVal(), ParameterType.Input);
        Parameter p8 = new Parameter(Types.BIGINT, info.getRoomkey(), ParameterType.Input);
        SqlHelper.execute(sql,p1,p2,p3,p4,p5,p6,p7,p8);
    }
    
    /**
     * 查下级玩家的贡献记录
     * @param limit 查询跳水
     * @param page 页数
     * @param clubid 俱乐部ID
     * @param uid 要查询的下级
     * @param pid 合伙人ID
     * @return
     */
    public static JSONObject findSubordinatePlayerContributionPage(int limit, Integer page, Integer clubid, Integer uid, Integer pid) {
        JSONObject jsonObject = new JSONObject();
        JSONArray array = new JSONArray();
        String countSql = "select count(id) from xy_club_contribution_log where cid="+clubid+" and pid="+pid+" and uid = "+uid+"";
        String sumSql = "select ifnull(sum(val),0) from xy_club_contribution_log where cid="+clubid+" and pid="+pid+" and uid = "+uid+"";
        String sql = "select `integral`,relevance_id,proportion,createdate,val from xy_club_contribution_log where cid="+clubid+" and pid="+pid+" and uid = "+uid+" ORDER BY createdate desc LIMIT ?,"+limit;
        Object scalar = SqlHelper.execScalar(countSql);
        int sumCount = Integer.parseInt(scalar.toString());
        int sumPage = (int) Math.ceil((double) sumCount/limit);
        page = page<1?1:page>sumPage?sumPage:page;
        page = page<1?1:page;
        Parameter p1 = new Parameter(Types.INTEGER, ((page-1)*limit), ParameterType.Input);
        CachedRowSet resultSet = SqlHelper.getResultSet(sql,p1);
        jsonObject.put("page",page);
        jsonObject.put("allpage",sumPage);
        jsonObject.put("allcount",sumCount);
        jsonObject.put("allContribution", SqlHelper.execScalar(sumSql));
        try {
            while (resultSet.next()) {
                JSONObject jsonData = new JSONObject();
                jsonData.put("integral", resultSet.getInt(1));
                jsonData.put("relevanceId",resultSet.getInt(2));
                jsonData.put("proportion",resultSet.getInt(3));
                jsonData.put("createdate",resultSet.getTimestamp(4).getTime());
                jsonData.put("val",resultSet.getInt(5));
                array.add(jsonData);
            }
        } catch (SQLException e) {
            LogManager.getInstance().errorLog(e);
        }
        jsonObject.put("array",array);
        return jsonObject;
    }
}
